//
//  UIImage+Extension.m
//  Baisijie
//
//  Created by 王建伟 on 2017/3/2.
//  Copyright © 2017年 RangGuangyu. All rights reserved.
//

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

+ (instancetype)bs_imageOriginalWithNamed:(NSString *)imageName {
    UIImage *image = [UIImage imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
+ (instancetype)bs_showTopImageWithImage:(UIImage *)image size:(CGSize)size {
    // 开启图形上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    
    // 将下载完的image对象绘制到图形上下文
    CGFloat height = size.width * image.size.height / image.size.width;
    [image drawInRect:CGRectMake(0, 0, size.width, height)];
    
    // 获取当前上下文的图片
    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    return returnImage;
}

+ (instancetype)bs_circleImageNamed:(NSString *)imageName {
    UIImage *image = [UIImage imageNamed:imageName];
    return [image bs_circleImage];
}
- (instancetype)bs_circleImage {
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    
    //计算裁剪尺寸
    CGRect frame = CGRectMake(0, 0, self.size.width, self.size.height);
    [[UIBezierPath bezierPathWithOvalInRect:frame] addClip];
    
    //绘图
    [self drawInRect:frame];
    
    // 获取当前上下文的图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    return image;
}

@end
